package com.ayoka.parenting.ui.login;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.ayoka.parenting.R;
import com.ayoka.parenting.widget.ProgressWebView;
import com.jincp.common.dialog.BaseDialogBuilder;
import com.jincp.common.dialog.BaseDialogFragment;
import com.jincp.common.utils.LiveDataBus;
import com.jincp.common.utils.UIUtils;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebViewClient;

/**
 * Created by jinchunping on 2019/1/23.
 */

public class WebViewVerifyDialog extends BaseDialogFragment implements ProgressWebView.ActivityView {

    public static WebViewVerifyDialog newInstance(Builder builder) {
        WebViewVerifyDialog dialog = new WebViewVerifyDialog();
        Bundle bundle = getArgumentBundle(builder);
        dialog.setArguments(bundle);
        return dialog;
    }

    private int code;
    private ProgressWebView progressWebview;

    private LinearLayout parent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LiveDataBus.getInstance().with("JSAndNative", String.class).observe(this, new android.arch.lifecycle.Observer<String>() {
            @Override
            public void onChanged(@Nullable String value) {
                if (!TextUtils.isEmpty(value)) {
                    dismissAllowingStateLoss();
                }
            }
        });
    }

    @Override
    protected void convertView(@Nullable View container, final BaseDialogFragment dialogFragment) {
        parent = container.findViewById(R.id.layout_container);
        if (progressWebview != null) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, UIUtils.dp2px(200));
            parent.addView(progressWebview, params);
            progressWebview.post(new Runnable() {
                @Override
                public void run() {
                    //通过java代码调用js方法
                    progressWebview.loadUrl("javascript:onNVCVCallback(" + code + ")");
                }
            });
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_webview_verify;
    }

    @Override
    public WebViewClient getWebViewClient() {
        return null;
    }

    @Override
    public WebChromeClient getWebChromeClient() {
        return null;
    }

    public void setWebView(ProgressWebView progressWebview) {
        this.progressWebview = progressWebview;
    }

    public void setCode(int bizCode) {
        this.code = bizCode;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if (parent != null) {
            parent.removeAllViews();
        }
        super.onDismiss(dialog);
    }

    public static class Builder extends BaseDialogBuilder<Builder, WebViewVerifyDialog> {

        @Override
        public WebViewVerifyDialog build() {
            return WebViewVerifyDialog.newInstance(this);
        }
    }

}